package org.cyz.tools.core.manager;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.cyz.tools.Application;
import org.cyz.tools.common.util.ImageUtil;

import java.io.IOException;
import java.net.URL;

/**
 * @author Chenyz
 * @title: WindowManager
 * @projectName RedisGUI
 * @description: 窗口管理器
 * @date 2020/10/2116:35
 */
public class WindowManager {

    //标题
    private String title;
    private static Parent root = null;
    //窗体是否置顶
    private boolean isTop=false;
    //fxml路径
    private String fxmlPath;
    public static Stage stage = new Stage();

    //窗体大小
    private double width=230;
    private double height=400;


    //坐标
    private static double xOffset = 0;
    private static double yOffset = 0;

    private boolean hideCloseBtn=false;

    public WindowManager(String fxmlPath){
        this.fxmlPath=fxmlPath;
    }


    public WindowManager setSize(double width,double height){
        this.width=width;
        this.height=height;
        return this;
    }

    public WindowManager isTop(boolean isTop){
        this.isTop=isTop;
        return this;
    }

    public WindowManager setTitle(String title){
        this.title=title;
        return this;
    }

    public Stage builder(){
        //删除最大化最小化按钮（你可以用stage.setResizable(false)来做到这一点;您还可以使用stage.initStyle(StageStyle.UNDECORATED)删除）
        if(!"UNDECORATED".equals(stage.getStyle().toString())){
            stage.initStyle(StageStyle.UNDECORATED);
            stage.initOwner(Application.stage);
        }

        try {
            URL _url = WindowManager.class.getClassLoader().getResource(this.fxmlPath);
            root = FXMLLoader.load(_url);
            //拖动
            root.setOnMousePressed(event -> {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            });
            root.setOnMouseDragged(event -> {
                stage.setX(event.getScreenX() - xOffset);
                stage.setY(event.getScreenY() - yOffset);
            });

            stage.setTitle(this.title);
            stage.setResizable(false);
            stage.setAlwaysOnTop(this.isTop);
            stage.getIcons().add(ImageUtil.getStartIcon("logo"));
            stage.setScene(new Scene(root, this.width, this.height));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stage;
    }


}
